package com.demo.controller;

import com.demo.dto.SysUserDTO;
import com.demo.dto.UserDTO;
import com.demo.entity.SysUser;
import com.demo.model.JsonMessage;
import com.demo.security.AuthUserDetail;
import com.demo.service.SysUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/admin/sysUser")
public class SysUserController {
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    @Autowired
    private SysUserService userService;


    @PostMapping("findUserIdByRoleId")
    @ResponseBody
    public JsonMessage findUserIdByRoleId() {
        JsonMessage message = new JsonMessage();
        List<UserDTO> list = userService.findUserIdByRoleId("002");
        message.setData(list);
        return message;
    }

    @PostMapping("updateStudentLeader")
    @ResponseBody
    public JsonMessage updateStudentLeader(String userId, String leaderId) {
        JsonMessage message = new JsonMessage();
        userService.updateStudentLeader(userId, leaderId);
        message.setData("ok");
        return message;
    }


    /**
     * 按角色查询用户
     *
     * @param pageIndex
     * @param pageSize
     * @param roleId
     * @return
     */
    @PostMapping("listByRole")
    @ResponseBody
    public JsonMessage listByRole(@RequestParam(defaultValue = "0") int pageIndex,
                                  @RequestParam(defaultValue = "10") int pageSize,
                                  String roleId, String userNumber) {
        JsonMessage message = new JsonMessage();
        PageInfo<SysUserDTO> page = userService.listByRole(pageIndex, pageSize, roleId, userNumber);
        message.setData(page);
        return message;
    }

    /**
     * 恢复账号
     *
     * @param userId
     * @return
     */
    @PostMapping("backUser")
    @ResponseBody
    public JsonMessage backUser(String userId) {
        JsonMessage message = new JsonMessage("更新成功", 200, true);
        userService.backUser(userId);
        return message;
    }

    /**
     * 停用账号
     *
     * @param loginName
     * @return
     */
    @PostMapping("remove")
    @ResponseBody
    public JsonMessage remove(String loginName) {
        JsonMessage message = new JsonMessage("删除成功", 200, true);
        userService.remove(loginName);
        return message;
    }

    @PostMapping("cleanUser")
    @ResponseBody
    public JsonMessage cleanUser(String loginName) {
        JsonMessage message = new JsonMessage("删除成功", 200, true);
        userService.cleanUser(loginName);
        return message;
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public JsonMessage addSysUser(@RequestBody SysUser user) {
        JsonMessage message = new JsonMessage();
        if (userService.confirmLoginName(user.getLoginName(), user.getTelephone(), user.getUserNumber()) == 0) {
            user.setUserId(UUID.randomUUID().toString());
            user.setCreateTime(new Date());
            user.setDisabled(0);
            userService.add(user);
        } else {
            message.setData(0);
        }
        return message;
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @PostMapping("getByUserId")
    @ResponseBody
    public JsonMessage getByUserId(String userId) {
        JsonMessage message = new JsonMessage("更新成功", 200, true);
        SysUser sysUser = userService.getByUserId(userId);
        message.setData(sysUser);
        return message;
    }

    /**
     * 更新员工信息
     *
     * @param user
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public JsonMessage updateSysUser(@RequestBody SysUser user) {
        JsonMessage message = new JsonMessage("更新成功", 200, true);
        userService.update(user);
        return message;
    }


    @PostMapping("list")
    @ResponseBody
    public JsonMessage list(@RequestParam(defaultValue = "0") int pageIndex,
                            @RequestParam(defaultValue = "10") int pageSize) {
        JsonMessage message = new JsonMessage();
        PageInfo<SysUser> page = userService.list(pageIndex, pageSize);
        message.setData(page);
        return message;
    }


    @PostMapping("password")
    @ResponseBody
    public JsonMessage password(Authentication authentication,
                                String loginName, String loginPass, String passOld) {
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        JsonMessage message = new JsonMessage();
        String pass = userService.pass(loginName);
        boolean f = passwordEncoder.matches(passOld, pass);
        if (f == true) {
            userService.password(loginName, loginPass);
            message.setData("更新成功");
        } else {
            message.setData("旧密码错误");
        }

        return message;
    }

    @PostMapping("passwordReset")
    @ResponseBody
    public JsonMessage passwordReset(String loginName, String loginPass) {
        JsonMessage message = new JsonMessage();
        userService.password(loginName, loginPass);
        message.setData("重置成功");
        return message;
    }


    @PostMapping("getByLoginName")
    @ResponseBody
    public JsonMessage getByLoginName(String userId) {
        JsonMessage message = new JsonMessage("更新成功", 200, true);
        SysUser sysUser = userService.getByLoginName(userId);
        message.setData(sysUser);
        return message;
    }


    @PostMapping("findFromUserID")
    @ResponseBody
    public JsonMessage findFromUserID(Authentication authentication) {
        JsonMessage message = new JsonMessage();
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        SysUser sysUser = userService.findFromUserID(detail.getUserId());
        message.setData(sysUser);
        return message;
    }


    @PostMapping("roleUpdate")
    @ResponseBody
    public JsonMessage roleUpdate(Authentication authentication, String userId, String roleId) {
        JsonMessage message = new JsonMessage();
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        if (detail.getUserId().equals(userId)) {
            userService.roleId(userId, roleId);
            message.setData("self");
        } else {
            userService.roleId(userId, roleId);
            message.setData("更改成功");
        }

        return message;
    }

}
